import 'dart:async';

import 'package:dim/dim.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanchong/path.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Splash extends StatefulWidget {
  @override
  _SplashState createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  // SharedPreferences _prefs;
  StreamSubscription<dynamic> _streamSubscription;
  String _token;
  Dim _dim = Dim();
  @override
  void initState() {
    // _init();
    super.initState();
    _getToken().then((String token) {
      // print("token");
      // print(token);
      _token = token;
    });
    countDown();
  }
  // _init() async {
  //   if (!mounted) return;
  //   if (_streamSubscription==null) {
      
  //   }
  // }
  @override
  Widget build(BuildContext context) {
    //默认 width : 1080px , height:1920px , allowFontScaling:false
    // ScreenUtil.instance = ScreenUtil.getInstance()..init(context);

//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 375, height: 812)..init(context);

//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
    // ScreenUtil.instance =
    //     ScreenUtil(width: 750, height: 1624, allowFontScaling: true)
    //       ..init(context);
    return Container(
      color: Colors.red,
//      child: new Text("hello world"),
    );
  }

  void countDown() {
    var _duration = new Duration(seconds: 3);
    new Future.delayed(_duration, _next);
  }

  void _next() {
    //判断是否登录，如果未登录，到登陆页面
    if (_token == null) {
      _goLogin();
    } else {
      _goHomePage();
    }
  }

  void _goLogin() {
    Navigator.of(context).pushReplacementNamed(LoginPath);
  }

  void _goHomePage() {
    Navigator.of(context).pushReplacementNamed(HomePath);
  }

  Future<String> _getToken() async {
    SharedPreferences _prefs = await SharedPreferences.getInstance();
    // String token =
    return _prefs.getString("token");
  }
}
